import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import changePageTitle from '../utils/changePageTitle'
import greenBook from "./greenBook";
//import nexmoe from "./nexmoe";
import store from '../store/index'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const routes = [
  ...greenBook,
  //...nexmoe,
  {path: '/test', name: 'test', component: () => import('@/views/Test/Test.vue')},
  {
    path: '/404',
    name:'404',
    component: () => import('../views/Error')
  },
  {
    path: '*',
    name: 'Error',
    component: () => import('../views/Error')
  }
]

const router = new VueRouter({
  mode: 'history',
  linkActiveClass: 'active',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savedPosition) {
    store.state.savedPosition = {}
    if (to.hash) {
      return {
        selector: decodeURIComponent(to.hash)
      }
    } else if (savedPosition) {
      store.state.savedPosition = savedPosition
      return savedPosition

      // setTimeout(() => {
      //   window.scrollTo(savedPosition.x, savedPosition.y)
      // }, 1000)

    } else {
      return {x: 0, y: 0}
    }
  },

})

router.beforeEach((to, from, next) => {
  changePageTitle(to.meta.title)
  next()
})

export default router
